﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi
{
    /// <summary>
    /// 树节点
    /// </summary>
    public interface ITreeNode<T>
    {
        /// <summary>
        /// 主键Id
        /// </summary>
        public object Id { get; set; }
        /// <summary>
        /// 父Id
        /// </summary>
        public object Pid { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        public List<T> Children { get; set; }
    }

    /// <summary>
    /// 树节点扩展
    /// </summary>
    public static class TreeNodeEx
    {
        /// <summary>
        /// 转为树结构
        /// </summary>
        /// <param name="treeNodes">树节点</param>
        /// <returns>树节点带子节点（Children）</returns>
        public static List<T> ToTree<T>(this List<T> treeNodes) where T : ITreeNode<T>
        {
            if (treeNodes == null || !treeNodes.Any())
                return treeNodes ?? new List<T>();
            if (treeNodes.Any(o => o.Id == o.Pid))
                throw new Exception("存在id和pid相同的数据，无限循环引用");

            var group = treeNodes.GroupBy(x => x.Pid).ToDictionary(x => x.Key, s => s.ToList());

            foreach (var item in treeNodes)
            {
                if (group.ContainsKey(item.Id))
                {
                    var treeNode = group[item.Id];
                    if (treeNode.Any(o => o.Id == item.Pid && o.Pid == item.Id))
                        throw new Exception("存在id和pid交叉引用(如：{Id='我是你',Pid='你是我'} 和 {Id='你是我',Pid='我是你'})");

                    if (item.Children == null)
                        item.Children = new List<T>();

                    item.Children.AddRange(treeNode);
                }
            }

            //查找顶级节点
            var ids = treeNodes.Select(o => o.Id).Distinct();
            var pids = treeNodes.Select(o => o.Pid).Distinct();
            var c = pids.Except(ids);
            return group.Where(o => c.Contains(o.Key)).SelectMany(o => o.Value).ToList();
        }

        /// <summary>
        /// 将指定的id节点转为树结构
        /// </summary>
        /// <param name="treeNodes">树节点</param>
        /// <param name="id">id节点</param>
        /// <returns>树节点带子节点（Children）</returns>
        public static T? ToTree<T>(this List<T> treeNodes, object id) where T : ITreeNode<T>
        {
            if (treeNodes == null || !treeNodes.Any())
                return default(T);
            if (treeNodes.Any(o => o.Id == o.Pid))
                throw new Exception("存在id和pid相同的数据，无限循环引用");

            var group = treeNodes.GroupBy(x => x.Pid).ToDictionary(x => x.Key, s => s.ToList());

            foreach (var item in treeNodes)
            {
                if (group.ContainsKey(item.Id))
                {
                    var treeNode = group[item.Id];
                    if (treeNode.Any(o => o.Id == item.Pid && o.Pid == item.Id))
                        throw new Exception("存在id和pid交叉引用(如：{Id='我是你',Pid='你是我'} 和 {Id='你是我',Pid='我是你'})");

                    if (item.Children == null)
                        item.Children = new List<T>();

                    item.Children.AddRange(treeNode);
                }
            }

            return treeNodes.FirstOrDefault(o => o.Id == id);
        }

        /// <summary>
        /// 转为树节点
        /// </summary>
        /// <param name="treeNodes">树节点</param>
        /// <returns>树节点带子节点（Children）</returns>
        public static List<T> ToTreeNode<T>(this List<T> treeNodes) where T : ITreeNode<T>
        {
            if (treeNodes == null || !treeNodes.Any())
                return treeNodes ?? new List<T>();

            List<T> trees = new List<T>();
            foreach (var item in treeNodes)
            {
                trees.Add(item);

                if (item.Children != null && item.Children.Any())
                {
                    var aaa = ToTreeNode(item.Children);
                    trees.AddRange(aaa);
                }
            }
            return trees;
        }
    }
}
